package cn.edu.neusoft.zw725.foodorder.model;

import cn.edu.neusoft.zw725.foodorder.bean.LoginBean;
import cn.edu.neusoft.zw725.foodorder.iface.LoginIface;
import cn.edu.neusoft.zw725.foodorder.listener.LoginListener;
import cn.edu.neusoft.zw725.foodorder.service.LoginService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by china on 2017/11/1.
 */

public class LoginModel implements LoginIface {
    private LoginService loginService;
    private Retrofit retrofit;
    private static final String BASE_URL = "http://60.205.189.39/";
    public LoginModel(){
        retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getLoginBean(String username, String userpass, final LoginListener loginListener) {
        loginService = retrofit.create(LoginService.class);
        Call<LoginBean> call = loginService.getLoginBean(username,userpass);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    loginListener.onResponse(response.body());
                }
                else{
                    loginListener.onFail("error");
                }
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                loginListener.onFail("error");
            }
        });
    }
}
